package com.xxx.base.rest;

import com.xxx.base.rest.response.RestRet;
import com.xxx.base.util.Log;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.util.UUID;

/**
 * 统一 接口 异常处理
 */
@RestControllerAdvice
public class RestExceptionHandler {

    final Log log = Log.of(getClass());

    @ExceptionHandler(HttpRequestMethodNotSupportedException.class)
    public RestRet exHandler(HttpRequestMethodNotSupportedException ex) {
        return RestRet.fail(ex.getMessage());
    }


    @ExceptionHandler(Exception.class)
    public RestRet exHandler(Exception ex) {
        String errorId = UUID.randomUUID().toString();
        log.error(ex, "errorId: " + errorId);
        return RestRet.fail(ex.getMessage()).setErrorId(errorId);
    }
}
